package com.ruoyi.patrol.utils;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.patrol.domain.AreaCodeMap;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：小杜
 * @date ：Created in 2023/5/18 21:20
 * @描述 ：返回地区code工具类
 */
public class AreaCodeMapUtils {
    public static Map<String, String> getAreaCode() throws IOException {
        ClassPathResource cp = new ClassPathResource("city");
        byte[] bytes = FileCopyUtils.copyToByteArray(cp.getInputStream());
        String s = new String(bytes, StandardCharsets.UTF_8);
        List<AreaCodeMap> areaCodeMaps = JSONArray.parseArray(s, AreaCodeMap.class);
        Map<String, String> map = new HashMap<>(100000);
        for (AreaCodeMap acm : areaCodeMaps) {
            for (AreaCodeMap.ChildrenBeanX child1 : acm.getChildren()) {
                List<AreaCodeMap.ChildrenBeanX.ChildrenBean> child2 = child1.getChildren();
                if (child2.isEmpty()) {
                    map.put(acm.getName() + child1.getName(), acm.getCode() + "0000," + child1.getCode() + "00");
                } else {
                    for (AreaCodeMap.ChildrenBeanX.ChildrenBean item : child2) {
                        String acmName = acm.getName();
                        if (acmName.contains("市")) {
                            map.put(acm.getName() + item.getName(), acm.getCode() + "0000," + child1.getCode() + "00," + item.getCode());
                        } else {
                            map.put(acm.getName() + child1.getName() + item.getName(), acm.getCode() + "0000," + child1.getCode() + "00," + item.getCode());
                        }
                    }
                }
            }
        }
        return map;
    }
}
